package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hey.lib.HeySpinner;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.RechargeBean;
import com.sskj.mine.asset.data.RechargeTypeBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/05/03
 */
@Route(path = RConfig.MINE_RECHARGE)//充币
public class RechargeActivity extends BaseActivity<RechargePresenter> {

    @BindView(R2.id.select_coin)
    HeySpinner selectCoin;
    @BindView(R2.id.qr_code)
    ImageView qrCode;
    @BindView(R2.id.address)
    TextView address;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @BindView(R2.id.copy_tv)
    TextView copyTv;
    @BindView(R2.id.right_tv)
    TextView rightTv;
    private Disposable typeDispo;
    private String coinType;
    private String pid;
    private BottomSheetDialog bottomSheetCoin;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_recharge;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    public void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_rechargeActivity1_j));
        selectCoin.setItemGravity(Gravity.CENTER);

        rightTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.mine_icon_history, 0);
        rightTv.setVisibility(View.VISIBLE);

        ClickUtil.click(rightTv, () -> ARouter.getInstance().build(RConfig.MINE_ASSET_RECORDS).withInt(Constans.TYPE, 1).navigation());

    }

    @Override
    public void initData() {
        mPresenter.getType();
    }

    public void refresh() {
        mPresenter.recharge(pid);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RechargeActivity.class);
        context.startActivity(intent);
    }

    public void updateType(List<RechargeTypeBean> data) {
        if (data == null || data.size() <= 0)
            return;
        pid = data.get(0).getPid();
        coinType = data.get(0).getMark();
        selectCoin.setText(coinType);
        refresh();
        typeDispo = Flowable.fromIterable(data)
                .map(RechargeTypeBean::getPname)
                .toList()
                .subscribe((strings, throwable) -> {
                    ClickUtil.click(selectCoin, () -> {
                        bottomSheetCoin = BottomSheetUtil.getBottomSheet(RechargeActivity.this, null, (recyclerView, position, view) -> {
                            bottomSheetCoin.dismiss();
                            selectCoin.setText(strings.get(position));
                            pid = data.get(position).getPid();
                            coinType = data.get(position).getPname();
                            refresh();
                        }, strings);
                        bottomSheetCoin.show();
                    });
//                    selectCoin.attachData(strings);
//                    selectCoin.setOnSelectListener(position -> {
//                        pid = data.get(position).getPid();
//                        coinType = data.get(position).getPname();
//                        refresh();
//                    });

                });
    }

    public void updateUI(RechargeBean data) {
        address.setText(data.getAddress());
        ImageUtil.setImage(data.getQrc(), qrCode);
        ClickUtil.click(copyTv, () -> {
            CopyUtil.copy(data.getAddress());
        });
        tvDesc.setText(String.format(App.INSTANCE.getString(R.string.mine_mine_activity_recharge27_j0),coinType));
    }
}
